﻿using System.Collections.Generic;
using Xenta.Api;
using Xenta.Entities;
using Xenta.Enums;
using Xenta.Security;
using Xenta.Utils;

namespace Xenta.Operations
{
    #region __General__

    /// <summary>
    /// Creates an item attribute.
    /// </summary>
    public class CreateSalesItemAttribute : CreateGenericAttribute<SalesItemAttributeEntity>
    {
    }

    /// <summary>
    /// Creates an item attribute.
    /// </summary>
    public class UpdateSalesItemAttribute : UpdateGenericAttribute<SalesItemAttributeEntity>
    {
    }

    /// <summary>
    /// Sets an item attribute.
    /// </summary>
    public class SetSalesItemAttribute : SetGenericAttribute<SalesItemAttributeEntity>,
        ISecuredOperation, IApiOperation
    {
        #region Properties

        /// <summary>
        /// Gets the collection of security policies.
        /// </summary>
        public IEnumerable<SecurityPolicy> SecurityPolicies
        {
            get
            {
                yield return new SecurityPolicy()
                    .IsAuthenticated()
                    .HasPermission("SALES");
                yield return new SecurityPolicy()
                    .IsAuthenticated()
                    .IsVerified()
                    .Rule((ctx, uow) =>
                    {
                        var seller = ctx.Seller(uow);
                        if(seller == null || !seller.Flags.HasFlag(SellerFlags.Active))
                            return false;
                        var item = uow.Execute(new GetSalesItem
                        {
                            EntityID = HolderID
                        }).Result;
                        if(item == null)
                            return false;
                        return item.Seller.Equals(seller);
                    });
            }
        }

        #endregion
    }

    /// <summary>
    /// Deletes the item attribute.
    /// </summary>
    public class DeleteSalesItemAttribute : DeleteGenericAttribute<SalesItemAttributeEntity>
    {
    }

    /// <summary>
    /// Gets the item attribute by identifier.
    /// </summary>
    public class GetSalesItemAttribute : GetGenericAttribute<SalesItemAttributeEntity>
    {
    }

    /// <summary>
    /// Gets the item attribute by the key.
    /// </summary>
    public class GetSalesItemAttributeByKey : GetGenericAttributeByKey<SalesItemAttributeEntity>
    {
    }

    /// <summary>
    /// Gets the item attribute value by key.
    /// </summary>
    public class GetSalesItemAttributeValueByKey : GetGenericAttributeValueByKey<SalesItemAttributeEntity>,
        IApiOperation
    {
    }

    #endregion

    #region __Bulk__

    /// <summary>
    /// Updates the item attributes from dictionary.
    /// </summary>
    public class BulkUpdateSalesItemAttributes : BulkUpdateGenericAttributes<SalesItemAttributeEntity>,
        ISecuredOperation, IApiOperation
    {
        #region Properties

        /// <summary>
        /// Gets the collection of security policies.
        /// </summary>
        public IEnumerable<SecurityPolicy> SecurityPolicies
        {
            get
            {
                yield return new SecurityPolicy()
                    .IsAuthenticated()
                    .HasPermission("SALES");
                yield return new SecurityPolicy()
                    .IsAuthenticated()
                    .IsVerified()
                    .Rule((ctx, uow) =>
                    {
                        var seller = ctx.Seller(uow);
                        if(seller == null || !seller.Flags.HasFlag(SellerFlags.Active))
                            return false;
                        var item = uow.Execute(new GetSalesItem
                        {
                            EntityID = HolderID
                        }).Result;
                        if(item == null)
                            return false;
                        return item.Seller.Equals(seller);
                    });
            }
        }

        #endregion
    }

    #endregion
}
